package com.ruoyi.oa.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.OaProject;
import com.ruoyi.oa.domain.OaPurchaseApply;
import com.ruoyi.oa.domain.bo.OaPurchaseApplyBo;
import com.ruoyi.oa.domain.vo.OaPurchaesPaymentVo;
import com.ruoyi.oa.domain.vo.OaPurchaseApplyVo;
import com.ruoyi.oa.service.IOaPurchaseApplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 采购申请
 *
 * @author ruoyi
 * @date 2023-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/purchaseApply")
public class OaPurchaseApplyController extends BaseController {

    private final IOaPurchaseApplyService iOaPurchaseApplyService;

    /**
     * 查询采购申请列表
     */
//    @SaCheckPermission("oa:purchaseApply:list")
    @GetMapping("/list")
    public TableDataInfo<OaPurchaseApplyVo> list(OaPurchaseApplyBo bo, PageQuery pageQuery) {
        return iOaPurchaseApplyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购申请列表
     */
//    @SaCheckPermission("oa:purchaseApply:export")
    @Log(title = "采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaPurchaseApplyBo bo, HttpServletResponse response) {
        List<OaPurchaseApplyVo> list = iOaPurchaseApplyService.queryList(bo);
        ExcelUtil.exportExcel(list, "采购单", OaPurchaseApplyVo.class, response);
    }

    /**
     * 获取采购申请详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:purchaseApply:query")
    @GetMapping("/{id}")
    public R<OaPurchaseApplyVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable String id) {
        return R.ok(iOaPurchaseApplyService.queryById(id));
    }

    /**
     * 通过项目名称查询采购申请
     * @param projectName
     * @return
     */
    @RequestMapping("/queryByProjectName")
    public R<OaPurchaseApplyVo> queryByProjectName(@RequestBody  Map<String,Object> map) {
        return R.ok(iOaPurchaseApplyService.queryByProjectName(map.get("projectName").toString()));
    }

    /**
     * 新增采购申请
     */
//    @SaCheckPermission("oa:purchaseApply:add")
    @Log(title = "采购申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaPurchaseApplyBo> add(@Validated(AddGroup.class) @RequestBody OaPurchaseApplyBo bo) {
        OaPurchaseApplyVo applyVo = iOaPurchaseApplyService.queryByProjectName(bo.getProjectName());
        if (ObjectUtil.isNotEmpty(applyVo)){
            return R.fail(bo.getProjectName()+"已经存在,不能重复申请.");
        }
        return R.ok(iOaPurchaseApplyService.insertByBo(bo));
    }

    /**
     * 修改采购申请
     */
//    @SaCheckPermission("oa:purchaseApply:edit")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaPurchaseApply> edit(@Validated(EditGroup.class) @RequestBody OaPurchaseApplyBo bo) {
        return R.ok(iOaPurchaseApplyService.updateByBo(bo));
    }

    /**
     * 删除采购申请
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:purchaseApply:remove")
    @Log(title = "采购申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaPurchaseApplyService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @Log(title = "获取没有采购流程的项目信息")
    @GetMapping("/getProjectList")
    public R<List<OaProject>> getProjectList() {
        return R.ok(iOaPurchaseApplyService.getProjectList());
    }
}
